
package com.creditease.tmsp.modules.sys.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.creditease.tmsp.common.config.Global;
import com.creditease.tmsp.common.persistence.IdGenerator;
import com.creditease.tmsp.common.utils.StringUtils;
import com.creditease.tmsp.common.web.BaseController;
import com.creditease.tmsp.modules.sys.entity.SysRole;
import com.creditease.tmsp.modules.sys.entity.SysRoleResource;
import com.creditease.tmsp.modules.sys.service.RoleResourceService;
import com.creditease.tmsp.modules.sys.service.RoleService;
import com.creditease.tmsp.modules.sys.service.SysResourceService;
import com.creditease.tmsp.modules.sys.service.UserRoleService;

/**
 * 角色Controller
 * @author 
 * @version 
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/sysrole")
public class SysRoleController extends BaseController {

	@Autowired
	private RoleService roleService;
	@Autowired
	private UserRoleService userRoleService;
	@Autowired
	private SysResourceService sysResourceService;
	@Autowired
	private RoleResourceService roleResourceService;
	
	@ModelAttribute
	public SysRole get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			SysRole role = roleService.getById(id);
	        Map<String, Object> params = new HashMap();
	        params.put("roleId", id);
			List<SysRoleResource> srrList = roleResourceService.find(params);
			StringBuffer sysResourceIds = new StringBuffer();
			for(SysRoleResource srr:srrList){
				sysResourceIds.append(srr.getResourceId()+",");
			}
			role.setSysResourceIds(sysResourceIds.toString());
			return role;
		}else{
			return new SysRole();
		}
	}
	
	@RequestMapping(value = {"list", ""})
	public String list(SysRole role, Model model) {
		List<SysRole> list = roleService.find(null);
		model.addAttribute("list", list);
		return "modules/sys/roleList";
	}

	@RequestMapping(value = "form")
	public String form(SysRole role, Model model) {

		model.addAttribute("role", role);
		model.addAttribute("sysResourceList", sysResourceService.findAllMenu());
		return "modules/sys/roleForm";
	}
	
	@RequestMapping(value = "save")
	public String save(SysRole role, Model model, String oldName, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, role)){
			return form(role, model);
		}
		if (!"true".equals(checkName(oldName, role.getName()))){
			addMessage(model, "保存角色'" + role.getName() + "'失败, 角色名已存在");
			return form(role, model);
		}
		//保存角色信息
		try {
			saveOrUpdate(roleService,role);
		} catch (Exception e) {
			e.printStackTrace();
		}
		//保存角色和菜单关系
		roleResourceService.deleteByRoleIdResourceId(null, role.getId());
		String resourceIds = role.getSysResourceIds();
		for(String resourceId:resourceIds.split(",")){
			SysRoleResource sr = new SysRoleResource();
			sr.setId(IdGenerator.getId());
			sr.setResourceId(resourceId);
			sr.setRoleId(role.getId());
			roleResourceService.save(sr);
		}
				
		addMessage(redirectAttributes, "保存角色'" + role.getName() + "'成功");
		return "redirect:"+Global.getAdminPath()+"/sys/sysrole/?repage";
	}

	@RequestMapping(value = "delete")
	public String delete(@RequestParam String id, RedirectAttributes redirectAttributes) {

		roleService.delete(id);
		addMessage(redirectAttributes, "删除角色成功");
		return "redirect:"+Global.getAdminPath()+"/sys/sysrole/?repage";
	}
	

	@RequestMapping(value = "usertorole")
	public String selectUserToRole(SysRole role, Model model) {
		model.addAttribute("role", role);
		//model.addAttribute("selectIds", role.getUserIds());
		return "modules/sys/selectUserToRole";
	}
	
	
	@RequestMapping(value = "outrole")
	public String outrole(String userId, String roleId, RedirectAttributes redirectAttributes) {
		SysRole role = roleService.getById(roleId);

		userRoleService.deleteByUserIdRoleId(userId, roleId);
		return "redirect:"+Global.getAdminPath()+"/sys/role/assign?id="+role.getId();
	}
	

	@ResponseBody
	@RequestMapping(value = "checkName/ajax")
	public String checkName(String oldName, String name) {
		if (name!=null && name.equals(oldName)) {
			return "true";
		} else if (name!=null && roleService.getByName(name) == null) {
			return "true";
		}
		return "false";
	}

}
